﻿// ReSharper disable InconsistentNaming
using VSSpeeder.Build;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tests.Unit.VSSpeeder
{

    [TestClass]
    public class LogParserTests
    {

        [TestMethod]
        public void ProcessOutputTest_Null_EmptyResult()
        {
            var result = ExecProcessOutput(null);
            Assert.AreEqual(0, result.ProcessedProjects);
        }



        [TestMethod]
        public void ProcessOutputTest_TestInAndOutput_NormalCase()
        {
            const int lineNumber = 1;
            const int columnNumber = 1;
            const string pathName = "X IRRELEVANTSTRING PATH\\XXX\\";
            const string filename = "X IRRELEVANTSTRING.cs";
            const string projectName = "X IRRELEVANTSTRING.csproj";
            const string errorMessage = "X ErrorMessage IRRELEVANTSTRING";

            var input = filename + "(" + lineNumber + "," + columnNumber + "): error CS1061:" + errorMessage + " [" + pathName + projectName + "]";

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(lineNumber, result.Tasks[0].Line);
            Assert.AreEqual(errorMessage, result.Tasks[0].Description);
            Assert.AreEqual(pathName + projectName, result.Tasks[0].Project);
            Assert.AreEqual(pathName + filename, result.Tasks[0].FileName);


        }


        [TestMethod]
        public void ProcessOutputTest_TestInAndOutput_MissingNumbers()
        {
            const string pathName = "X IRRELEVANTSTRING PATH\\XXX\\";
            const string filename = "X IRRELEVANTSTRING.cs";
            const string projectName = "X IRRELEVANTSTRING.csproj";
            const string errorMessage = "X ErrorMessage IRRELEVANTSTRING";

            var input = filename + ": error CS1061:" + errorMessage + " [" + pathName + projectName + "]";

            OutputResult result = ExecProcessOutput(input);
                        
            Assert.AreEqual(errorMessage, result.Tasks[0].Description);
            Assert.AreEqual(pathName + projectName, result.Tasks[0].Project);
            Assert.AreEqual(pathName + filename, result.Tasks[0].FileName);


        }

        [TestMethod]
        public void ProcessOutputTest_TestInAndOutput_MissingSquareBrackets()
        {
            const int lineNumber = 1;
            const int columnNumber = 1;
            const string filename = "X IRRELEVANTSTRING.cs";
            const string errorMessage = "X ErrorMessage IRRELEVANTSTRING";

            var input = filename + "(" + lineNumber + "," + columnNumber + "): error CS1061:" + errorMessage ;

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(lineNumber, result.Tasks[0].Line);
            Assert.AreEqual(errorMessage, result.Tasks[0].Description);
            //Assert.AreEqual(pathName + projectName, result.Tasks[0].Project);
            //Assert.AreEqual(pathName + filename, result.Tasks[0].FileName);


        }





        [TestMethod]
        public void ProcessOutputTest_Bug_MissingFilenameInSquareBrackets()
        {
            OutputResult result = ExecProcessOutput(@"X.cs(17,32): error CS1729: 'VSSpeeder.Build.LogParser' does not contain a constructor that takes 1 arguments");
            Assert.AreEqual(17, result.Tasks[0].Line);

        }

        [TestMethod]
        public void ProcessOutputTest_Bug_MissingLineColumnNumbers()
        {
            var solutionName = "XXSOLNAME.sln.metaproj";
            var pathname = "XXXPATH";
            string input = solutionName + @" : error MSB3202: The project file ""d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeedster\VSSpeedster.csproj"" was not found. [" +  pathname + @"\VSSpeeder.sln]";

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(pathname + "\\" + solutionName, result.Tasks[0].FileName);
        }



        [TestMethod]
        public void ProcessOutputTest_Bug_No002()
        {
            OutputResult result = ExecProcessOutput(@"X.cs(141,72): error CS1061: 'string' does not contain a definition for 'IsNotNullOrEmpty' and no extension method 'IsNotNullOrEmpty' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) [D:\DataPeterGfader\ProjectsTFS-APLUS\Auction\trunk\RealTimeAuctionsTestClients\SilverlightClient\SilverlightClient.csproj]");
            Assert.AreEqual(141, result.Tasks[0].Line);
        }



        [TestMethod]
        public void ProcessOutputTest_Bug_No003()
        {
            OutputResult result = ExecProcessOutput(@"X.cs(101,72): error CS1061: 'string' does not contain a definition for 'IsNotNullOrEmpty' and no extension method 'IsNotNullOrEmpty' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) [D:\DataPeterGfader\ProjectsTFS-APLUS\Auction\trunk\RealTimeAuctionsTestClients\SilverlightClient\SilverlightClient.csproj]");
            Assert.AreEqual(101, result.Tasks[0].Line);
        }


        [TestMethod]
        public void ProcessOutputTest_Bug_No004_1()
        {
            string input = @"d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeeder.sln.metaproj : error MSB3202: The project file ""d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeedster\VSSpeedster.csproj"" was not found. [d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeeder.sln]";

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(1, result.Tasks.Count);
        }

        [TestMethod]
        public void ProcessOutputTest_Bug_No004_2()
        {
            string input = @"d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeeder.sln.metaproj : error MSB3202: The project file ""d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeedster\VSSpeedster_IntegrationTests\VSSpeedster_IntegrationTests.csproj"" was not found. [d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\VSSpeeder.sln]";

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(1, result.Tasks.Count);
        }


        [TestMethod]
        public void ProcessOutputTest_Bug_No004_3()
        {
            string input = @"LogParserTests.cs(48,21): error CS0111: Type 'Tests.Unit.VSSpeeder.LogParserTests' already defines a member called 'ProcessOutputTest_Bug_No003' with the same parameter types [d:\DataPeterGfader\ProjectsTFS-AtHome\VSSpeedster\vsspeedster\Code\Tests.Unit.VSSpeeder\Tests.Unit.VSSpeeder.csproj]";

            OutputResult result = ExecProcessOutput(input);

            Assert.AreEqual(48, result.Tasks[0].Line);
        }








        private static OutputResult ExecProcessOutput(string input)
        {
            LogParser target = new LogParser();
            return target.ProcessOutput(input);
        }



    }
}

// ReSharper restore InconsistentNaming